package com.xzy.controller;

import com.alibaba.fastjson.JSON;
import com.xzy.pojo.Message;
import com.xzy.pojo.MiXueResult;
import com.xzy.pojo.SelectVo;
import com.xzy.pojo.User;
import com.xzy.service.MessageService;
import com.xzy.utils.HttpTool;
import org.apache.http.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by css on 2018/8/21.
 */
@RestController
@RequestMapping("/receive/")
public class ReseiveDataController {
    @Autowired
    private MessageService messageService;
    @Autowired
    private HttpTool httpTool;

    @RequestMapping(value = "login",method = RequestMethod.GET)
    @ResponseBody
    public void login(String msg, HttpServletResponse response){
        try {
            response.getWriter().write(msg);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @RequestMapping(value = "login",method = RequestMethod.POST)

    public void postLogin(HttpServletRequest request,HttpServletResponse response){
        MessageQuery messageQuery=null;
        try {
            String json=request.getReader().lines().collect(Collectors.joining());
            messageQuery = JSON.parseObject(json, MessageQuery.class);
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            messageQuery.getMsg().setTime(simpleDateFormat.format(new Date()));
        } catch (IOException e) {
            e.printStackTrace();
        }
        messageService.insertMessage(messageQuery.getMsg());
        response.setStatus(200);
    }
    @RequestMapping(value = "getData")
    @ResponseBody
    public MiXueResult getData(String dsId){
        if(dsId==null){
            return MiXueResult.build(0,"参数为空");
        }
        SelectVo selectVo = new SelectVo();
        selectVo.setDs_id(dsId);
        List<Message> list = messageService.selectAvg(selectVo);
        return MiXueResult.ok(list);
    }
    @RequestMapping(value = "getName")
    @ResponseBody
    public String getName(HttpSession session){
        User user = (User)session.getAttribute("user");
        if(user==null){
            return null;
        }
        return user.getName();
    }
    @RequestMapping(value = "getNowData")
    @ResponseBody
    public Message getNowData(String dsId){
        if (dsId==null){
            return null;
        }
        Message nowData = messageService.getNowData(dsId);

        Double value = nowData.getValue();
        String s = value.toString();
        int i = s.lastIndexOf(".");
        String time = nowData.getTime();
        if(time.indexOf(" ")>0)
        nowData.setTime(time.split(" ")[1]);
        String substring=null;
        if(s.length()>(i+5)){
            substring = s.substring(0, s.lastIndexOf(".")+5);
            nowData.setValue(new Double(substring));
        }

        return nowData;
    }
    @RequestMapping(value = "isBuyCss")
    @ResponseBody
    public boolean isBuyCss(HttpSession session){
        User user = (User)session.getAttribute("user");
        if(user==null){
            return false;
        }
        return messageService.isBuy(user.getId());
    }
    @RequestMapping(value = "getStateFeng")
    @ResponseBody
    public Boolean getStateFeng(){
        String s = httpTool.sendGet("http://api.heclouds.com/nbiot", "imei=awz710121&obj_id=3311&obj_inst_id=0");
        Boolean flag=false;
        if (s.contains("true")){
            flag= true;
        }
        return flag;
    }
    @RequestMapping(value = "getStateWater")
    @ResponseBody
    public Boolean getStateWater(){
        String s = httpTool.sendGet("http://api.heclouds.com/nbiot", "imei=awz710121&obj_id=3311&obj_inst_id=1");
        Boolean flag=false;
        if (s.contains("true")){
            flag= true;
        }
        return flag;
    }
    @RequestMapping(value = "OnOffFeng")
    @ResponseBody
    public Boolean OnOffFeng(Boolean b,HttpSession session){
        User user=(User)session.getAttribute("user");
        if (user==null){
            return false;
        }
        String s = httpTool.sendPost("http://api.heclouds.com/nbiot", "imei=awz710121&obj_id=3311&obj_inst_id=0&mode=1", "{\"data\": [{\"res_id\":5850,\"val\":" + b + "}]}");
        Boolean flag=false;
        if (s.contains("succ")){
            flag= true;
        }
        return flag;
    }
    @RequestMapping(value = "OnOffWater")
    @ResponseBody
    public Boolean OnOffWater(Boolean b,HttpSession session){
        User user=(User)session.getAttribute("user");
        if (user==null){
            return false;
        }
        String s = httpTool.sendPost("http://api.heclouds.com/nbiot", "imei=awz710121&obj_id=3311&obj_inst_id=1&mode=1", "{\"data\": [{\"res_id\":5850,\"val\":" + b + "}]}");
        Boolean flag=false;
        if (s.contains("succ")){
            flag= true;
        }
        return flag;
    }
}
